import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:kwai/kwai.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _kwaiPlugin = Kwai();
  String _initDesc = '';
  String _loginState = '';
  Map _callback = {};

  @override
  void initState() {
    super.initState();
    _kwaiPlugin.authorLoginCallback().listen((event) {
      setState(() {
        _callback = event.toJson();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            TextButton(
                onPressed: () {
                  _kwaiPlugin
                      .registerKwaiApp('ks685673047210945076',
                          'https://kuaishou.com/sdksample/')
                      .then((value) {
                    if (kDebugMode) {
                      print(value);
                    }
                    setState(() {
                      _initDesc = value;
                    });
                  });
                },
                child: const Text('初始化')),
            TextButton(
                onPressed: () {
                  _kwaiPlugin.authorLogin(scope: 'user_info').then((value) {
                    if (kDebugMode) {
                      print(value);
                    }
                    setState(() {
                      _loginState = value;
                    });
                  });
                },
                child: const Text('授权登录')),
            Text.rich(TextSpan(text: '结果：\n', children: [
              TextSpan(text: '初始化结果：$_initDesc\n'),
              TextSpan(text: '授权登录结果：$_loginState\n'),
              TextSpan(text: '回调结果：$_callback\n')
            ]))
          ],
        ),
      ),
    );
  }
}
